//
//  MZAniShowView.m
//  MZAnimationDemo
//
//  Created by Chris Yang on 2016/11/24.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "MZDemoSupprot.h"
#import "UIView+MZLayout.h"

#import "MZAniShowView.h"

@implementation MZAniShowView

- (id)init {
    
    self = [super init];
    
    if (self) {
        [self configSelf];
        [self allocSubviews];
        [self configSubviews];
        [self positionSubviews];
    }
    
    return self;
}

- (void)configSelf {
    [self setBackgroundColor:mzDemoRandColor(0.3)];
}

- (void)allocSubviews {
    
    _imageView = [[UIImageView alloc] init];
    
    _restoreButton = [MZDemoSupprot mzDemoButton];
    _performButton = [MZDemoSupprot mzDemoButton];
    
    _aniPauseButton = [MZDemoSupprot mzDemoButton];
    _aniContinueButton = [MZDemoSupprot mzDemoButton];
    
    _aniDurationSettingButton = [MZDemoSupprot mzDemoButton];
}

- (void)configSubviews {
    
    /* Image View */
    [_imageView setImage:[UIImage imageNamed:@"test_girl.png"]];
    [_imageView setClipsToBounds:YES];
    
    /* Restore Button */
    [_restoreButton setTitle:@"还原" forState:UIControlStateNormal];
    [_restoreButton setTag:MZAnimationOperationTypeRestore];
    [_restoreButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    /* Perform Button */
    [_performButton setTitle:@"测试" forState:UIControlStateNormal];
    [_performButton setTag:MZAnimationOperationTypePerform];
    [_performButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    /* Animation Pause Button */
    [_aniPauseButton setTitle:@"暂停" forState:UIControlStateNormal];
    [_aniPauseButton setTag:MZAnimationOperationTypePause];
    [_aniPauseButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    /* Animation Continue Button */
    [_aniContinueButton setTitle:@"继续" forState:UIControlStateNormal];
    [_aniContinueButton setTag:MZAnimationOperationTypeContinue];
    [_aniContinueButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    /* Animation Duratino Setting Button */
    [_aniDurationSettingButton setTitle:@"秒" forState:UIControlStateNormal];
    [_aniDurationSettingButton setTag:MZAnimationOperationTypeSetDuration];
    [_aniDurationSettingButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_imageView];
    [self addSubview:_restoreButton];
    [self addSubview:_performButton];
    [self addSubview:_aniPauseButton];
    [self addSubview:_aniContinueButton];
    [self addSubview:_aniDurationSettingButton];
}

- (void)positionSubviews {
    
    CGRect tmpRect = CGRectZero;
    
    /* Image View */
    tmpRect = [self mzCenterRectInSubviewWithWidth:150 height:150];
    tmpRect.origin.y -= 50;
    [_imageView setFrame:tmpRect];
    [_imageView.layer setCornerRadius:tmpRect.size.width / 2];
    
    /* Restore Button */
    tmpRect = [self mzRectInSubviewWithWidth:100 height:100 toLeft:30 toBottom:60];
    [_restoreButton setFrame:tmpRect];
    [_restoreButton.layer setCornerRadius:tmpRect.size.width / 2];
    
    /* Perform Button */
    tmpRect = [self mzRectInSubviewWithWidth:100 height:100 toRight:30 toBottom:60];
    [_performButton setFrame:tmpRect];
    [_performButton.layer setCornerRadius:tmpRect.size.width / 2];
    
    /* Animation Pause Button */
    tmpRect = [self mzCenterRectInSubviewWithWidth:80 height:45 toBottom:10];
    tmpRect.origin.x -= 50;
    [_aniPauseButton setFrame:tmpRect];
    [_aniPauseButton.layer setCornerRadius:5];
    
    /* Animation Continue Button */
    tmpRect = [self mzCenterRectInSubviewWithWidth:80 height:45 toBottom:10];
    tmpRect.origin.x += 50;
    [_aniContinueButton setFrame:tmpRect];
    [_aniContinueButton.layer setCornerRadius:5];
    
    /* Animation Duration Setting Button */
    tmpRect = [self mzRectInSubviewWithWidth:45 height:45 toRight:10 toBottom:10];
    [_aniDurationSettingButton setFrame:tmpRect];
    [_aniDurationSettingButton.layer setCornerRadius:tmpRect.size.width / 2];
}

- (void)layoutSubviews {
    [self positionSubviews];
}

#pragma mark - Target-Action Pair

- (void)buttonClicked:(UIButton *)button {
    if ([_delegate respondsToSelector:@selector(showView:buttonClickedWithOpType:)]) {
        [_delegate showView:self buttonClickedWithOpType:button.tag];
    }
}

#pragma mark - MISC

#pragma mark - User Interface

- (void)restore {
    
    [_imageView.layer removeAllAnimations];
    [self positionSubviews];
}

- (void)updateDuration:(float)duration {
    NSString *tmpStr = [NSString stringWithFormat:@"%.1fs", duration];
    [_aniDurationSettingButton setTitle:tmpStr forState:UIControlStateNormal];
}

@end
